<?php                                                                         

class Markets {

        const Portland = 1;
        const Boise = 2;   
        const SanJose = 3; 
        const SanDiego = 5;
        const Seattle = 6; 
        const Phoenix = 8; 
        const Socal = 18;  

        public static $marketId2db = array(
                1 => 'PDX_HomeQuest',      
                2 => 'Boise_HomeQuest',    
                3 => 'SanJose_HomeQuest',  
                5 => 'SanDiego_HomeQuest', 
                6 => 'Seattle_HomeQuest',  
                8 => 'Phoenix_HomeQuest',  
                18 => 'socal'              
        );                                 

        static public function getActiveMarkets() {

                $key = 'activeMarkets';
                $cache = Cache::getInstance();
                $res = $cache->fetch( $key ); 

                if( $res == false ) {
                        $args = array('op' => 'getMarkets');
                        $req = new HQ_Request($args);       
                        $res - $req->execute();             

                        $cache->store( $key, $res );

                }

                return $res;

        }

        public static function getLastUpdate( $marketId ) {

                $marketId = Clean::int( $marketId );
                $key = "marketUpdate-$marketId";
                $cache = Cache::getInstance();
                $res = $cache->fetch( $key );

                // fix this - toby
                if( $res == false ) {
                        $args = array('op' => 'getLastUpdate');
                        $req = new HQ_Request($args);
                        $res = $req->execute();

                        $cache->store( $key, $res, 300 ); // Refresh every 5 min.

                }

                return $res;

        }

        public static function getMarketData( $marketId ) {

                $marketId = Clean::int( $marketId );
                $key = "marketData-$marketId";
                $cache = Cache::getInstance();
                $res = $cache->fetch($key);

                if( $res == false ) {

                        $args = array('op' => 'getMarketData');
                        $req = new HQ_Request($args);
                        $res = $req->execute();

                        if( count( $res ) == 0 ) {
                                throw new Exception( "Failed to load market data: $marketId." );
                        }

                        $cache->store( $key, $res );

                }

                return $res;

        }

};

?>

